package com.ywk.base.dataStructure.queue;

//单链表实现队列（先进先出）
public class LinkedListQueueDemo {

	public static void main(String[] args) {
		QueueNode sn1=new QueueNode(10);
		QueueNode sn2=new QueueNode(20);
		QueueNode sn3=new QueueNode(30);
		LinkedListQueue lq=new LinkedListQueue();
		lq.entry(sn1);
		lq.entry(sn2);
		lq.entry(sn3);
		lq.showQueue();
		System.out.print("出列：");
		System.out.println(lq.outGoing());
		System.out.println("出队列之后：");
		lq.showQueue();
	}

}


class LinkedListQueue{
	private QueueNode head=new QueueNode();//头结点

	//入队，新的节点添加到最后
	public void entry(QueueNode qn) {
		QueueNode p=head;
		while(true) {
			if(p.next==null) {
				p.next=qn;
				break;
			}
			p=p.next;
		}
	}
	//出队，每次出队的节点都是头结点的下一个节点
	public QueueNode outGoing() {
		QueueNode value=head.next;
		head.next=head.next.next;
		return value;
	}
	
	public void showQueue() {
		QueueNode p=head.next;
		if(p==null) {
			System.out.println("链表为空");
			return;
		}
		while(true) {
			System.out.println(p);
			if(p.next==null) {
				break;
			}
			p=p.next;
		}
	}
}

class QueueNode{
	private int value;
	public QueueNode next;
	
	public QueueNode() {}
	
	public QueueNode(int value) {
		super();
		this.value = value;
	}

	public int getValue() {
		return value;
	}

	public void setValue(int value) {
		this.value = value;
	}

	public QueueNode getNext() {
		return next;
	}

	public void setNext(QueueNode next) {
		this.next = next;
	}

	@Override
	public String toString() {
		return "QueueNode [value=" + value + "]";
	}
	
	
}
























